# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: P_λ.py
  @time: 2022/3/18 10:13
  @desc:光谱扫描？？
"""
import time

from matplotlib import pyplot as plt
from scipy.signal import savgol_filter
from ATS.hardwarescan import conf_file_manager
from ATS.application.LASER_App.N7744C_App import N7744C_App
from ATS.application.LASER_App.L8164B_App import L8164B_App
from ATS.application.LASER_App.N7786C_App import N7786C_App
from ATS.util.save_data_2_csv_util import saveData2CSV
from ATS.util.calcu_step_util import get_step_list
# from plans.test.logs.refrence_data import y1_data
from ATS.switchdriver.LightSwitchDriver import LightSwitchDriver
import csv

name = "SS2"
n7744c_app = N7744C_App(name)

name = "SS3"
l8164b_app = L8164B_App(name)

name = "SS1"
n7786c_app = N7786C_App(name)


def spectral_scan(start_wave=1260, stop_wave=1360, step_wave=1, power_value=0, n7744_power_range=-30):
    global y_data
    light_inst = LightSwitchDriver().instance()
    if light_inst.is_connected:
        light_inst.set_all_switch("01", "05")
        time.sleep(1)
    else:
        light_inst.connect()
    x_data = get_step_list(start_wave, stop_wave, step_wave)
    points = len(x_data)
    l8164b_app.set_power_unit()
    l8164b_app.set_power_value(power_value)
    l8164b_app.set_sweep_mode()
    print(l8164b_app.query_sweep_pmax(start_wave, stop_wave))
    l8164b_app.set_repeat_mode()
    l8164b_app.set_wave_sweep_speed()
    l8164b_app.set_start_stop_step_wave(start_wave, stop_wave, step_wave)
    l8164b_app.trigger_config()
    l8164b_app.trigger_output()
    l8164b_app.trigger_input()
    l8164b_app.output_on()
    l8164b_app.set_wave_sweep_state()

    # n7744c_app.reset()
    n7744c_app.set_power_unit()
    n7744c_app.set_power_range(n7744_power_range)
    n7744c_app.set_auto_gain()
    n7744c_app.set_power_atim()
    n7744c_app.trigger_input_mode()
    n7744c_app.set_power_wave_length()
    n7744c_app.set_logging_points(points)
    n7744c_app.set_logging_state()
    n7744c_app.tigger_status()
    while True:
        state = n7744c_app.get_func_state()
        if state == "LOGGING_STABILITY,COMPLETE":
            y_data = n7744c_app.get_func_res()
            break
    # n7786c_app.stop_logging_polcontroller()
    # n7786c_app.set_stabilization_mode(0)
    l8164b_app.stop_sweep()
    return x_data, y_data


def plot_curve1(x_data, y_data):
    plt.plot(x_data, y_data, "b")
    plt.xlabel("WaveLength")
    plt.ylabel("Value")
    plt.grid(True)
    plt.show()


# 平滑曲线
def savgol2(x_data, y_data):
    plt.plot(x_data, y_data, "k")
    y_smooth = savgol_filter(y_data, 201, 3, mode="nearest")
    plt.plot(x_data, y_smooth, "b")
    plt.xlabel("WaveLength")
    plt.ylabel("Value")
    plt.grid(True)
    plt.show()


def savgol(x_data, y_data, title):
    plt.figure(figsize=(20, 20), dpi=100)
    fig, ax = plt.subplots()
    ax.set_title(f"{title} P-λ curve")
    ax.plot(x_data, y_data, label="raw_data")
    y_smooth = savgol_filter(y_data, 201, 3, mode="nearest")
    ax.plot(x_data, y_smooth, label="smooth_data")

    ax.set_xlabel("WaveLength")
    ax.set_ylabel("Value")
    ax.grid(True)
    ax.legend()
    plt.savefig(f"{title}.png")
    plt.show()


x_data, y_data = spectral_scan()
# # y_data = [float(y_data[i]) - float(y1_data[i]) for i in range(len(y_data))]
# # x_index = x_data.index(1280)
# # x_data = x_data[x_index:]
# # y_data = y_data[x_index:]
savgol(x_data, y_data, "demo")
#
saveData2CSV("data.csv", ["wavelength", "power"], [[x_data, y_data]])
